package com.git_demo.controller;

import com.git_demo.bean.Classes;
import com.git_demo.bean.Student;
import com.git_demo.service.ClassService;
import com.git_demo.service.StudentService;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author 金振宇
 * @describe
 */
@Controller
@RequestMapping("stu")
public class StudentController {

    private Logger logger = LoggerFactory.getLogger(StudentController.class);

    @Reference(url = "dubbo://localhost:20880")
    private StudentService studentService;

    @Reference(url = "dubbo://localhost:20880")
    private ClassService classService;

    @RequestMapping("findAllStu")
    public String findAllStu(Model model){
        try {
            logger.info("查询所有学生");
            List<Student> studentList = studentService.selectAllStudent();
            model.addAttribute("studentList",studentList);
            return "index";
        }catch (Exception e){
            logger.error("查询所有学生异常",e);
        }
        return "redirect:/erro.html";
    }

    @RequestMapping("deleteStu")
    public String deleteStu(Integer stuId){
        try {
            logger.info("根据ID删除学生");
            studentService.deleteStuById(stuId);
            return "redirect:/stu/findAllStu";
        }catch (Exception e){
            logger.error("根据ID删除学生失败",e);
        }
        return "redirect:/erro.html";
    }
    @RequestMapping("/toAdd")
    public String toAdd(Model model){
        try {
            logger.info("去学生添加页面");
            List<Classes> classesList = classService.findAllClasses();
            model.addAttribute("classesList",classesList);
            return "/add.html";
        } catch (Exception e) {
           logger.error("去学生添加页面失败",e);
        }
        return "redirect:/erro.html";
    }

    @RequestMapping("updateStu")
    public String updateStu(Student student){
        try {
            logger.info("修改学生");
            System.out.println(student);
            studentService.updateStu(student);
            return "redirect:/stu/findAllStu";
        }catch (Exception e){
            logger.error("根据ID删除学生失败",e);
        }
        return "redirect:/erro.html";
    }

    @RequestMapping("/toUpdate")
    public String toUpdate( Model model, Integer stuId){
        try {
            List<Classes>   classesList = classService.findAllClasses();
            Student student = studentService.findStudentById(stuId);
            model.addAttribute("stu",student);
            model.addAttribute("classesList",classesList);
            return "/update.html";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/erro.html";

    }

    @RequestMapping("/add")
    public String add(Student student){
        try {
            studentService.addStu(student);
            return "redirect:/stu/findAllStu";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/erro.html";
    }
}
